<template>
    <div>
        <!-- 面包屑导航区域 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片视图区域 -->
        <el-card>
            <!-- 添加分类区域 -->
            <el-row>
                <el-col>
                    <el-button type="primary" @click="addCateDialog">添加分类</el-button>
                </el-col>
            </el-row>
            <!-- 角色列表 -->
            <tree-table class="treeTable" :data="cateList" :columns="columns" show-index index-text="#" border :expand-type="false" :selection-type="false">
              <!-- 是否有效 -->
              <template slot="isOk" scope="scope">
                <i class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color:lightgreen"></i>
                <i class="el-icon-error" v-else style="color:red"></i>
              </template>
              <!-- 排序 -->
              <template slot="order" scope="scope">
                <el-tag v-if="scope.row.cat_level===0">一级</el-tag>
                <el-tag v-else-if="scope.row.cat_level===1" type="success">二级</el-tag>
                <el-tag v-else type="warning">三级</el-tag>
              </template>
              <!-- 操作 -->
              <template slot="opt">
                <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
              </template>
            </tree-table>
            <!-- 分页 -->
             <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryInfo.pagenum"
              :page-sizes="[3, 5, 10, 15]"
              :page-size="queryInfo.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
            </el-pagination>
        </el-card>
        <!-- 添加分类对话框 -->
        <el-dialog title="添加分类" :visible.sync="addCatedialogVisible" width="50%" @close="addCateDialogClose">
          <el-form :model="addCateForm" :rules="addCateRules" ref="addCateRef" label-width="100px">
            <el-form-item label="分类名称" prop="cat_name">
              <el-input v-model="addCateForm.cat_name"></el-input>
            </el-form-item>
            <el-form-item label="父级名称">
              <el-cascader v-model="selectedCate" :options="parentCateList"
                :props="cateProps" @change="parentCateChange" clearable>
              </el-cascader>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="addCatedialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addCate">确 定</el-button>
          </span>
        </el-dialog>
    </div>
</template>
<script>
export default {
  data() {
    return {
      // 查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 商品分类的数据列表
      cateList: [],
      // 数据总条数
      total: 0,
      // 为table指定columns的定义
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          // 将当前列定义为模板列
          type: 'template',
          // 当前这列使用的模板名称
          template: 'isOk'
        },
        {
          label: '排序',
          // 将当前列定义为模板列
          type: 'template',
          // 当前这列使用的模板名称
          template: 'order'
        },
        {
          label: '操作',
          // 将当前列定义为模板列
          type: 'template',
          // 当前这列使用的模板名称
          template: 'opt'
        }
      ],
      // 添加分类对话框
      addCatedialogVisible: false,
      addCateForm: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      addCateRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      parentCateList: [],
      // 级联选择框配置选项
      cateProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true,
        expandTrigger: 'hover'
      },
      // 选中项
      selectedCate: []
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    // 获取商品分类数据
    async getCateList() {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取商品分类失败')
      // cateList赋值
      this.cateList = res.data.result
      this.total = res.data.total
      this.$message.success('获取商品分类成功')
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum
      this.getCateList()
    },
    // 打开添加分类对话框
    addCateDialog() {
      this.getParentCateList()
      this.addCatedialogVisible = true
    },
    // 获取添加分类的父级分类列表
    async getParentCateList() {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取父级分类数据失败')
      this.$message.success('获取父级分类数据成功')
      this.parentCateList = res.data
    },
    parentCateChange() {
      // console.log(this.selectedCate)
      if (this.selectedCate.length > 0) {
        this.addCateForm.cat_pid = this.selectedCate[this.selectedCate.length - 1]
        this.addCateForm.cat_level = this.selectedCate.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    // 点击按钮，添加新的分类
    async addCate() {
      console.log(this.addCateForm)
      this.$refs.addCateRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.addCateForm)
        if (res.meta.status !== 201) return this.$message.error('添加分类失败')
        this.$message.success('添加分类成功')
        this.getCateList()
        this.addCatedialogVisible = false
      })
    },
    // 关闭添加分类对话框
    addCateDialogClose() {
      this.$refs.addCateRef.resetFields()
      this.selectedCate = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    }
  }
}
</script>
<style lang="scss" scoped>
.treeTable{
  margin-top: 15px;
}
.el-cascader{
  width: 100%
}
</style>
